\defmodule {CIRProcessEuler}

This class represents a \emph{CIR} process
as in \class{CIRProcess}, but
the process is generated using the simple Euler scheme
\begin{equation}
   X(t_j) - X(t_{j-1}) = \alpha(b - X(t_{j-1}))(t_j - t_{j-1}) +
      \sigma \sqrt{(t_j - t_{j-1})X(t_{j-1})}\, Z_j
                                    \label{eq:cir-seq-euler}
\end{equation}
where $Z_j \sim N(0,1)$. This is a good approximation only for small
time intervals $t_j - t_{j-1}$.

\bigskip\hrule\bigskip

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{code}
\begin{hide}
/*
 * Class:        CIRProcessEuler
 * Description:  
 * Environment:  Java
 * Software:     SSJ 
 * Copyright (C) 2001  Pierre L'Ecuyer and Universite de Montreal
 * Organization: DIRO, Universite de Montreal
 * @author       
 * @since

 * SSJ is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License (GPL) as published by the
 * Free Software Foundation, either version 3 of the License, or
 * any later version.

 * SSJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * A copy of the GNU General Public License is available at
   <a href="http://www.gnu.org/licenses">GPL licence site</a>.
 */
\end{hide}
package umontreal.iro.lecuyer.stochprocess;\begin{hide}
import umontreal.iro.lecuyer.rng.*;
import umontreal.iro.lecuyer.probdist.*;
import umontreal.iro.lecuyer.randvar.*;

\end{hide}

public class CIRProcessEuler extends StochasticProcess \begin{hide} {
    protected NormalGen    gen;
    protected double       alpha,
                           beta,
                           sigma;
    // Precomputed values
    protected double[]     alphadt,
                           sigmasqrdt;
\end{hide}
\end{code}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection* {Constructors}
\begin{code}

   public CIRProcessEuler (double x0, double alpha, double b, double sigma,
                           RandomStream stream) \begin{hide} {
      this (x0, alpha, b, sigma, new NormalGen (stream, new NormalDist()));
   }\end{hide}
\end{code}
\begin{tabb} Constructs a new \texttt{CIRProcessEuler} with parameters
$\alpha =$ \texttt{alpha}, $b$, $\sigma =$ \texttt{sigma} and initial value
$X(t_{0}) =$ \texttt{x0}. The normal variates $Z_j$ will be
generated by inversion using the stream \texttt{stream}.
\end{tabb}
\begin{code}

   public CIRProcessEuler (double x0, double alpha, double b, double sigma,
                           NormalGen gen) \begin{hide} {
      this.alpha = alpha;
      this.beta  = b;
      this.sigma = sigma;
      this.x0    = x0;
      this.gen   = gen;
   }\end{hide}
\end{code}
\begin{tabb} The normal variate generator \texttt{gen} is specified directly
instead of specifying the stream.
 \texttt{gen} can use another method than inversion.
\end{tabb}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection* {Methods}
\begin{code}\begin{hide}

   public double nextObservation() {
      double xOld = path[observationIndex];
      double x;
      x = xOld + (beta - xOld) * alphadt[observationIndex]
           + sigmasqrdt[observationIndex] * Math.sqrt(xOld) * gen.nextDouble();
      observationIndex++;
      if (x >= 0.0)
         path[observationIndex] = x;
      else
         path[observationIndex] = 0.;
      return x;
   }

\end{hide}
   public double nextObservation (double nextTime) \begin{hide} {
      double previousTime = t[observationIndex];
      double xOld = path[observationIndex];
      observationIndex++;
      t[observationIndex] = nextTime;
      double dt = nextTime - previousTime;
      double x = xOld + alpha * (beta - xOld) * dt
           + sigma * Math.sqrt (dt*xOld) * gen.nextDouble();
      if (x >= 0.0)
         path[observationIndex] = x;
      else
         path[observationIndex] = 0.;
      return x;
   }\end{hide}
\end{code}
\begin{tabb} Generates and returns the next observation at time $t_{j+1} =$
 \texttt{nextTime}, using the previous observation time $t_{j}$ defined earlier
(either by this method or by \texttt{setObservation\-Times}),
as well as the value of the previous observation $X(t_j)$.
\emph{Warning}: This method will reset the observations time $t_{j+1}$
for this process to \texttt{nextTime}. The user must make sure that
the $t_{j+1}$ supplied is $\geq t_{j}$.
\end{tabb}
\begin{code}

   public double nextObservation (double x, double dt) \begin{hide} {
      x = x + alpha * (beta - x) * dt +
          sigma * Math.sqrt (dt*x) * gen.nextDouble();
      if (x >= 0.0)
         return x;
      return 0.0;
   }\end{hide}
\end{code}
\begin{tabb} Generates an observation of the process in \texttt{dt} time units,
assuming that the process has value $x$ at the current time.
Uses the process parameters specified in the constructor.
Note that this method does not affect the sample path of the process
stored internally (if any).
\end{tabb}
\begin{hide}\begin{code}

   public double[] generatePath() {
      double x;
      double xOld = x0;
      for (int j = 0; j < d; j++) {
          x = xOld + (beta - xOld) * alphadt[j]
              + sigmasqrdt[j] * Math.sqrt(xOld) * gen.nextDouble();
          if (x < 0.0)
              x = 0.0;
          path[j + 1] = x;
          xOld = x;
      }
      observationIndex = d;
      return path;
   }

   public double[] generatePath (RandomStream stream) {
        gen.setStream (stream);
        return generatePath();
   }
\end{code}
\begin{tabb} Generates a sample path of the process at all observation times,
 which are provided in array \texttt{t}.
 Note that \texttt{t[0]} should be the observation time of \texttt{x0},
 the initial value of the process, and \texttt{t[]} should have at least $d+1$
 elements (see the \texttt{setObservationTimes} method).
\end{tabb}\end{hide}
\begin{code}

   public void setParams (double x0, double alpha, double b, double sigma) \begin{hide} {
      this.alpha = alpha;
      this.beta  = b;
      this.sigma = sigma;
      this.x0    = x0;
      if (observationTimesSet) init(); // Otherwise not needed.
   }\end{hide}
\end{code}
\begin{tabb}
Resets the parameters $X(t_{0}) =$ \texttt{x0}, $\alpha =$ \texttt{alpha},
 $b =$ \texttt{b} and $\sigma =$ \texttt{sigma} of the process.
\emph{Warning}: This method will recompute some quantities stored internally,
which may be slow if called too frequently.
\end{tabb}
\begin{code}

   public void setStream (RandomStream stream) \begin{hide} {
      gen.setStream (stream);
   }\end{hide}
\end{code}
\begin{tabb}
Resets the random stream of the normal generator to \texttt{stream}.
\end{tabb}
\begin{code}

   public RandomStream getStream() \begin{hide} {
      return gen.getStream ();
   }\end{hide}
\end{code}
\begin{tabb}
Returns the random stream of the normal generator.
\end{tabb}
\begin{code}

   public double getAlpha() \begin{hide} { return alpha; }\end{hide}
\end{code}
\begin{tabb}
Returns the value of $\alpha$.
\end{tabb}
\begin{code}

   public double getB() \begin{hide} { return beta; }\end{hide}
\end{code}
\begin{tabb}
Returns the value of $b$.
\end{tabb}
\begin{code}

   public double getSigma() \begin{hide} { return sigma; }\end{hide}
\end{code}
\begin{tabb}
Returns the value of $\sigma$.
\end{tabb}
\begin{code}

   public NormalGen getGen() \begin{hide} { return gen; }\end{hide}
\end{code}
\begin{tabb}
Returns the normal random variate generator used.
The \texttt{RandomStream} used for that generator can be changed via
\texttt{getGen().setStream(stream)}, for example.
\end{tabb}
\begin{code} \begin{hide}

    // This is called by setObservationTimes to precompute constants
    // in order to speed up the path generation.
    protected void init() {
       super.init();
       alphadt = new double[d];
       sigmasqrdt = new double[d];
       double dt;
       for (int j = 0; j < d; j++) {
           dt = t[j+1] - t[j];
           alphadt[j]      = alpha * (dt);
           sigmasqrdt[j]   = sigma * Math.sqrt (dt);
       }
    }\end{hide}
\end{code}
\begin{code}\begin{hide}
} \end{hide}
\end{code}
